package grape.crm.service.company.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.common.service.trans.ITransService;
import grape.crm.service.company.po.Company;
import grape.common.service.common.IBaseTreeService;

/**
 * <p>
 * 公司表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
public interface ICompanyService extends IBaseTreeService<Company> , ITransService<String,String> {

    public static final String trans_company_name = "trans_company_name";
    public static final String trans_company_code = "trans_company_code";
    public static final String trans_company_parent_name = "trans_company_parent_name";

    /**
     * 根据编码查询
     * @param code
     * @return
     */
    default Company getByCode(String code) {
        assertParamNotEmpty(code,"code 不能为空");
        Company company = new Company();
        company.setCode(code);
        return getOne(company);
    }
    /**
     * 判断编码是否已存在
     * @param code
     * @return
     */
    default boolean existCode(String code){
        assertParamNotEmpty(code,"code 不能为空");
        Company comp = new Company();
        comp.setCode(code);
        int r = count(Wrappers.query(comp));
        return r > 0;
    }
    @Override
    default boolean support(String type){
        return isEqualAny(type,trans_company_code, trans_company_name, trans_company_parent_name);
    }

    @Override
    default String trans(String type, String key){
        if (isEqual(type,trans_company_parent_name)) {
            Company parent = getParent(key);
            if (parent != null) {
                return parent.getName();
            }
        }else{
            Company po = getById(key);
            if (po != null) {
                if (isEqual(type,trans_company_name)) {
                    return po.getName();
                }else if (isEqual(type,trans_company_code)) {
                    return po.getCode();
                }
            }

        }
        return null;
    }
}
